$(function() {
    init();

    function init() {
        eventList();
    }

    function eventList() {
        $("#register_btn").on("tap", function() {
            var user_txt = $("#user_txt").val().trim();
            var pwd_txt = $("#pwd_txt").val().trim();
            var pwd_txt2 = $("#pwd_txt2").val().trim();
            var yz_inp = $("#yz_inp").val().trim();
            var isChk = $("#agree_chk").prop("checked");
            if (!user_txt || !$.checkPhone(user_txt)) {
                mui.toast("手机号不合法");
                // console.log(123);
                return;
            }
            if (!pwd_txt || pwd_txt.length < 6) {
                mui.toast("密码不合法");
                return;
            }
            if (pwd_txt != pwd_txt2) {
                mui.toast("密码不一致");
                return;
            }
            if (!yz_inp || yz_inp.length != 6) {
                mui.toast("验证码不合法");
                return;
            }
            if (!isChk) {
                mui.toast("请勾选协议!");
                return;
            }
            var params = {
                username: user_txt,
                password: pwd_txt,
                mobile: user_txt,
                vCode: yz_inp
            };
            $.post(location.origin + "/user/register", params, function(ret) {
                if (ret && ret.success) {
                    mui.toast("注册成功");
                    setTimeout(function() {
                        location.href = "./index.html";
                    }, 2000);
                } else {
                    mui.toast(ret.message);
                    return;
                }
            })
        });
        $("#yz_btn").on("tap", function() {
            var user_txt = $("#user_txt").val().trim();
            if (!user_txt || !$.checkPhone(user_txt)) {
                mui.toast("手机号不合法");
                // console.log(123);
                return;
            }
            $.get(location.origin + "/user/vCode", function(ret) {
                console.log(ret);
                $("#yz_btn").attr("disabled", "disabled");
                var times = 5;
                $("#yz_btn").html(times + "秒后重新获取");
                var timeId = setInterval(function() {
                    times--;
                    $("#yz_btn").html(times + "秒后重新获取");
                    if (times == 0) {
                        clearInterval(timeId);
                        $("#yz_btn").html("获取验证码");
                        $("#yz_btn").removeAttr("disabled");
                    }
                }, 1000)
            })
        })
    }
})